package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DeviceDeptMapper;
import com.eastjet.equipment.mapper.DeviceUserMapper;
import com.eastjet.equipment.mapper.DeviceWarehouseMapper;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.service.DeviceSupplierService;
import com.eastjet.equipment.service.DeviceWarehouseService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.equipment.util.PageUtil;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨博文
 * @description 针对表【device_warehouse(仓库表)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class DeviceWarehouseServiceImpl extends ServiceImpl<DeviceWarehouseMapper, DeviceWarehouse>
        implements DeviceWarehouseService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceWarehouseMapper deviceWarehouseMapper;
    @Autowired
    private DeviceDeptMapper deviceDeptMapper;
    @Autowired
    private DeviceUserMapper deviceUserMapper;
    @Autowired
    private DeviceRulesService deviceRulesService;
    @Autowired
    private CodeRules codeRules;

    @Override
    public Page<DeviceWarehouse> getWarehouseList(int page, int size, String condition) {
        // 定义Redis中的键
        String redisKey = "WarehouseList:";
        System.out.println("WarehouseList-condition:"+condition);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("WarehouseList-从Redis中获取数据");
            List<DeviceWarehouse> warehouseList;
            if (condition != null  && !condition.isEmpty() && condition !="") {
                System.out.println(2);
                warehouseList = deviceWarehouseMapper.selectByCondition(condition); // 获取所有数据
            } else {
                System.out.println(3);
                warehouseList   = (List<DeviceWarehouse>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(warehouseList, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询warehouseList");
            List<DeviceWarehouse> warehouseList;
            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition !="") {
                System.out.println(5);
                warehouseList = deviceWarehouseMapper.selectByCondition(condition);
            } else {
                System.out.println(6);
                condition="";
                warehouseList = deviceWarehouseMapper.selectByCondition(condition); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, warehouseList);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(warehouseList, page, size);
        }
    }

    @Override
    public String addWarehouse(DeviceWarehouse deviceWarehouse, String TableName) {
        System.out.println("deviceWarehouse:" + deviceWarehouse.toString());
        List<DeviceRules> list= deviceRulesService.getDeviceRulesByTableName(TableName);
        if (list.size() == 0)
        {
            //返回错误信息
            return "DeviceRules Table is not have the TableName";
        }else
        {
            System.out.println("1564654654654564654564654564564");
            // 调用代码规则
            deviceWarehouse.setCode(codeRules.codeRules(list));
        }
        try {
            // 尝试将数据插入数据库
            if (deviceWarehouseMapper.addWarehouse(deviceWarehouse) > 0) {
                // 定义Redis的键
                String redisKey = "WarehouseList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceWarehouse> deviceSuppliers = deviceWarehouseMapper.selectByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSuppliers);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "增加成功";
            } else {
                return "增加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "增加失败";
        }
    }

    @Override
    public String updateWarehouse(DeviceWarehouse deviceWarehouse) {
        System.out.println("deviceWarehouse:" + deviceWarehouse.toString());
        try {
            // 尝试将数据插入数据库
            if (deviceWarehouseMapper.updateWarehouse(deviceWarehouse) > 0) {
                // 定义Redis的键
                String redisKey = "WarehouseList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceWarehouse> deviceSuppliers = deviceWarehouseMapper.selectByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSuppliers);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }

    @Override
    public String deleteWarehouse(DeviceWarehouse deviceWarehouse) {
        System.out.println("deviceWarehouse:" + deviceWarehouse.toString());
        try {
            // 尝试将数据插入数据库
            if (deviceWarehouseMapper.deleteWarehouse(deviceWarehouse) > 0) {
                // 定义Redis的键
                String redisKey = "WarehouseList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceWarehouse> deviceSuppliers = deviceWarehouseMapper.selectByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSuppliers);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "删除成功";
            } else {
                return "删除失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "删除失败";
        }
    }

    @Override
    public String updateWarehouseState(DeviceWarehouse deviceWarehouse) {
        System.out.println("deviceWarehouse:" + deviceWarehouse.toString());
        try {
            // 尝试将数据插入数据库
            if (deviceWarehouseMapper.updateWarehouseState(deviceWarehouse) > 0) {
                // 定义Redis的键
                String redisKey = "WarehouseList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceWarehouse> deviceSuppliers = deviceWarehouseMapper.selectByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSuppliers);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }

    @Override
    public Page<DeviceDept> getDeptList(int page, int size, String condition) {
        // 定义Redis中的键
        String redisKey = "DeptList:";
        System.out.println("DeptList-condition:"+condition);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("WarehouseList-从Redis中获取数据");
            List<DeviceDept> deviceDepts;
            if (condition != null  && !condition.isEmpty() && condition !="") {
                System.out.println(2);
                deviceDepts = deviceDeptMapper.selectList(null); // 获取所有数据
            } else {
                System.out.println(3);
                deviceDepts   = (List<DeviceDept>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(deviceDepts, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询warehouseList");
            List<DeviceDept> deviceDepts;
            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition !="") {
                System.out.println(5);
                deviceDepts = deviceDeptMapper.selectList(null);
            } else {
                System.out.println(6);
                condition="";
                deviceDepts = deviceDeptMapper.selectList(null); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, deviceDepts);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(deviceDepts, page, size);
        }
    }

    @Override
    public Page<DeviceUser> getUserList(int page, int size, String condition) {
        // 定义Redis中的键
        String redisKey = "UserList:";
        System.out.println("UserList-condition:"+condition);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("WarehouseList-从Redis中获取数据");
            List<DeviceUser> deviceUsers;
            if (condition != null  && !condition.isEmpty() && condition !="") {
                System.out.println(2);
                deviceUsers = deviceUserMapper.selectList(null); // 获取所有数据
            } else {
                System.out.println(3);
                deviceUsers   = (List<DeviceUser>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(deviceUsers, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询warehouseList");
            List<DeviceUser> deviceUsers;
            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition !="") {
                System.out.println(5);
                deviceUsers = deviceUserMapper.selectList(null);
            } else {
                System.out.println(6);
                condition="";
                deviceUsers = deviceUserMapper.selectList(null); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, deviceUsers);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(deviceUsers, page, size);
        }
    }


}




